__author__ = 'Muscle'

from mui import *
from mui.Widgets import *
import time
import mui.color as color


class MyWindow(MUIWindow):
    def __init__(self, **kwargs):
        MUIWindow.__init__(self, **kwargs)
        self.background = 'dialog\\volume_bg.png |(10,10,10,10)'

        # for FPS
        self.__fpsCount = 0
        self.__fpsTimeStart = 0
        self.__fosTimeEnd = 0
        self.fpsText = MUIText(self, right = self.width-20, top = 20, width = 200, height = 40, text = "FPS: 0", fontSize = 20, fontColor = color.blue, fontWeight = 'Bold', blind = True)

        self.bindData('width', self.fpsText, 'right', dir = '->', converter =  lambda x: x-20 )
        self.bind('Mouse RUp', self.showPopup)
        self.bindCmd('item 1', self.onReceiveCommand, 'item 1', noarg = True)
        self.bindCmd('item 2', self.onReceiveCommand, 'item 2', noarg = True)
        self.bindCmd('item 3', self.onReceiveCommand, 'item 3', noarg = True)
        self.bindCmd('item 4', self.onReceiveCommand, 'item 4', noarg = True)
        self.bindCmd('citem 1', self.onReceiveCommand, 'citem 1', noarg = True)
        self.bindCmd('citem 2', self.onReceiveCommand, 'citem 2', noarg = True)

    def onReceiveCommand(self, cmd):
        print 'onReceiveCommand:', cmd
        

    def showPopup(self, comp, x, y, buttons, modifiers):
        print 'showPopup'
        popmenu = MUIPopupMenu(self, left = x, top = y, width = 10, height = 10, opacity = 0.5, bgColor = color.blue, autosize = True, autofit = True)

        item1 = MenuItem(popmenu, width = 100, height = 40, opacity = 0.5, text = 'item ' + str(1), command = 'item 1')
        item2 = MenuItem(popmenu, width = 100, height = 40, opacity = 0.5, text = 'item ' + str(2), command = 'item 2')

        item3 = MenuItem(popmenu, width = 100, height = 40, opacity = 0.5, text = 'item ' + str(3), command = 'item 3')
        item4 = MenuItem(popmenu, width = 100, height = 40, opacity = 0.5, text = 'item ' + str(4), command = 'item 4')

        citem1 =  MenuItem(item2, name = 'kid1', width = 100, height = 40, opacity = 0.5, text = 'child item ' + str(1), command = 'citem 1')
        citem2 =  MenuItem(item2, name ='kid2', width = 100, height = 40, opacity = 0.5, text = 'child item ' + str(2), command = 'citem 2')
        popmenu.doModal()


    def on_paint(self):
        if (self.__fpsCount == 0):
            self.__fpsTimeStart = time.clock()
        super(MyWindow, self).on_paint()
        self.__fpsCount += 1

        if (self.__fpsCount >= 120):
            self.__fpsTimeEnd = time.clock()
            diffTime = self.__fpsTimeEnd - self.__fpsTimeStart
            fps = self.__fpsCount / diffTime
            self.fpsText.text = "FPS: " + str(int(fps))
            self.__fpsCount = 0



def main():
    mui.muiInit(main)
    g_DXDevice = muiDX.CDXDevice()
    w = MyWindow(width = 1024, height = 768, dxDevice = g_DXDevice)
    w.ShowWindow(1)
    muiApp.run()
    g_DXDevice.Release()
    mui.muiUnInit()



main()